using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;

namespace CodeStory.WebControls
{
	internal class HierarchyData : System.Web.UI.IHierarchyData
	{
		private ITreeGridDataObject dataObject;
		private ITreeGridDataObject[] childrenDataObjects;

		public HierarchyData(ITreeGridDataObject dataObject) {
			this.dataObject = dataObject;
			childrenDataObjects = dataObject.GetChildren();
		}

		public IHierarchicalEnumerable GetChildren() {
			return new HierarchicalCollection(childrenDataObjects);
		}

		public IHierarchyData GetParent() {
			ITreeGridDataObject data = dataObject.GetParent(dataObject.ParentPath);
			return new HierarchyData(data);
		}

		public bool HasChildren {
			get { return childrenDataObjects != null && childrenDataObjects.Length > 0; }
		}
		public string Path {
			get { return dataObject.Path; }
		}
		public object Item {
			get { return dataObject.DataObject; }
		}
		public string Type {
			get { return dataObject.DataObject.GetType().ToString(); }
		}
	}

	internal class HierarchicalCollection : System.Web.UI.IHierarchicalEnumerable
	{
		private ITreeGridDataObject[] dataObjects;

		public HierarchicalCollection(ITreeGridDataObject[] dataObjects) {
			this.dataObjects = dataObjects;
		}

		public IHierarchyData GetHierarchyData(object enumeratedItem) {
			return new HierarchyData((ITreeGridDataObject)enumeratedItem);
		}

		public IEnumerator GetEnumerator() {
			foreach (ITreeGridDataObject o in dataObjects) {
				yield return o;
			}
		}
	}

	internal class MyHierarchicalDataSourceView : HierarchicalDataSourceView
	{
		string path = string.Empty;
		IHierarchyDataSelector selector;

		public MyHierarchicalDataSourceView(string path, IHierarchyDataSelector selector) {
			this.path = path;
			this.selector = selector;
		}

		public override IHierarchicalEnumerable Select() {
			return new HierarchicalCollection(selector.SelectData(path));
		}
	}

	public class TreeGridDataSource : System.Web.UI.IHierarchicalDataSource
	{
		IHierarchyDataSelector selector;
		public TreeGridDataSource(IHierarchyDataSelector selector) {
			this.selector = selector;
		}

		private EventHandlerList events;
		protected EventHandlerList Events {
			get {
				if (events == null) {
					events = new EventHandlerList();
				}
				return events;
			}
		}
		private static readonly object DataSourceChangedEventObject = new object();
		public event EventHandler DataSourceChanged {
			add { Events.AddHandler(DataSourceChangedEventObject,value); }
			remove { Events.RemoveHandler(DataSourceChangedEventObject,value); }
		}
		protected virtual void OnDataSourceChanged() {
			EventHandler handler = (EventHandler)Events[DataSourceChangedEventObject];
			if (handler != null) {
				handler(this,EventArgs.Empty);
			}
		}

		public HierarchicalDataSourceView GetHierarchicalView(string viewPath) {
			return new MyHierarchicalDataSourceView(viewPath,this.selector);
		}
	}
}